package zenupdaterapp.listadapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import zenupdaterapp.ui.R;

import java.util.List;

public class ExtrasListAdapter<T> extends ArrayAdapter<T> {
    private final LayoutInflater _inflater;

    public ExtrasListAdapter(Context context, List<T> objects) {
        super(context, android.R.layout.simple_spinner_item, objects);
        _inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position, convertView, parent);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ExtrasListViewWrapper wrapper;
        if (row == null) {
            row = _inflater.inflate(R.layout.itemtemplate_extraslist, null);
            wrapper = new ExtrasListViewWrapper(row);
            row.setTag(wrapper);
        } else {
            wrapper = (ExtrasListViewWrapper) row.getTag();
        }
        return row;
    }
}

//Class that Holds the Ids, so we have not to call findViewById each time which costs a lot of ressources
class ExtrasListViewWrapper
{
	private View base;
	private TextView label = null;
	private ImageView image = null;

	public ExtrasListViewWrapper(View base)
	{
		this.base = base;
	}

	public TextView getTextView()
	{
		if (label == null)
		{
			label = (TextView)base.findViewById(R.id.txtExtrasDisplay);
		}
		return label;
	}

	public ImageView getImage()
	{
		if (image == null)
		{
			image = (ImageView)base.findViewById(R.id.imgExperimentalStable);
		}
		return image;
	}
}